var win = Titanium.UI.currentWindow;

var db = Titanium.Database.open('dagboksDB');

var data = [];
 
/*var rows = db.execute('SELECT * FROM dagbok');

while (rows.isValidRow())
{
   data.push(Ti.UI.createTableViewRow({title:rows.fieldByName('NAME'),hasChild:false}));
   data.push({text:rows.fieldByName('POST')});
   rows.next();
}*/


var rows = db.execute('SELECT * FROM dagbok LEFT JOIN child ON dagbok.CHILDID = child.ID ORDER BY POSTDATE DESC');

  while (rows.isValidRow()) {
    var rowLength = rows.fieldByName('POST').length;
    var postWidth = (Ti.Platform.displayCaps.platformWidth -10);
    var calcRows = Math.ceil(rowLength/(postWidth/8));
    var length = calcRows*20+40;
    var row = Ti.UI.createTableViewRow({height:length});
    row.add(Ti.UI.createLabel({text:rows.fieldByName('RUBRIK'), left:10, top:5, height:'auto', width:300, font:{fontSize:18, fontFamily:'Marker Felt', fontWeight:'bold'}}));
    row.add(Ti.UI.createLabel({text:rows.fieldByName('NAME') + ': ' + rows.fieldByName('POSTDATE'), left:10, top:27, height:'auto', width:300, font:{fontSize:10}}));
    row.add(Ti.UI.createLabel({text:rows.fieldByName('POST'), left:10, top:42, height:'auto', width:300}));
    data.push(row);
    rows.next();
   
    Ti.API.debug(JSON.stringify([
    rowLength, postWidth, calcRows, length  
    ]));
  }



rows.close();

var tableview = Titanium.UI.createTableView({
        data:data,
        weidth:'auto',
        minRowHeight:80,
        //width:320,
  top:5,
  borderWidth:1,
  borderRadius:2,
  borderColor:"#ccc",
  backgroundColor:"black"

});

win.add(tableview);


//menyknapp - editas i menu.js, inte h?r!
var menu = Titanium.UI.Android.OptionMenu.createMenu();
  Titanium.include("menu.js");
